package com.campus.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.common.core.domain.AjaxResult;
import com.campus.mapper.BillboardMapper;
import com.campus.pojos.domain.BillBoard;
import com.campus.pojos.domain.dto.BillBoardDto;
import com.campus.pojos.domain.dto.BillBoardQueryDto;
import com.campus.pojos.domain.vo.BillBoardVo;
import com.campus.result.PageResult;
import com.campus.service.BillboardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@Transactional
public class BillboardServiceImpl extends ServiceImpl<BillboardMapper, BillBoard> implements BillboardService {

    @Autowired
    BillboardMapper billboardMapper;
    /**
     * 编辑公告栏
     * @return
     */
    public void edit(BillBoardDto dto) {
        BillBoard billBoard = getById(dto.getId());
        if(dto.getContent() != null){
            billBoard.setContent(dto.getContent());
        }
        if(dto.getShow() != null){
            billBoard.setShow(dto.getShow() == 1);
            if(dto.getShow() == 1) {
                billboardMapper.setShow(0);
            }
        }
        updateById(billBoard);
    }

    /**
     * 公告栏条件查询
     *
     * @param dto
     * @return
     */
    @Override
    public AjaxResult getList(BillBoardQueryDto dto) {
        if(dto.getPage() == null) dto.setPage(1);
        if(dto.getPageSize() == null) dto.setPageSize(3);
        Page<BillBoard> page = new Page<>(dto.getPage(),dto.getPageSize());

        QueryWrapper<BillBoard> qw = new QueryWrapper<>();
        if(dto.getId() != null){
            qw.eq("id", dto.getId());
        }
        if(dto.getContent() != null){
            qw.like("content",dto.getContent());
        }
        page = billboardMapper.selectPage(page, qw);
        ArrayList<BillBoardVo> billBoardVos = new ArrayList<>();
        for (BillBoard billBoard : page.getRecords()) {
            BillBoardVo billBoardVo = new BillBoardVo();
            BeanUtils.copyProperties(billBoard, billBoardVo);
            billBoardVo.setShow(billBoard.getShow() ? 1 : 0);
            billBoardVos.add(billBoardVo);
        }
        return AjaxResult.success("success", new PageResult(page.getTotal(),billBoardVos));
    }

    /**
     * 修改公告是否展示
     *
     * @param id
     * @param show
     * @return
     */
    @Override
    public AjaxResult status(Integer id, Integer show) {
        BillBoard billBoard = getById(id);
        billBoard.setShow(show == 1);
        if(show == 1){
                billboardMapper.setShow(0);
        }
        updateById(billBoard);
        return AjaxResult.success();
    }

    @Override
    public void saveBillboard(BillBoardDto dto) {
        BillBoard billBoard = new BillBoard();
        if(dto.getShow() == null){
            dto.setShow(0);
        }
        billBoard.setShow(dto.getShow() == 1);
        if(dto.getShow() == 1){
            billboardMapper.setShow(0);
        }
        billBoard.setCreateTime(DateTime.now());
        billBoard.setContent(dto.getContent());
        save(billBoard);
    }
}
